package com.yilin.tms.data.controller;

import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.commons.utils.UserUtil;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.NoLoginSecurity;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.entity.data.etc.EtcOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.truck.TruckVehicle;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.remote.IVehicleRemote;
import com.yilin.tms.data.service.IEtcService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * @author Administrator
 * 交科etc
 */
@RestController
@RequestMapping("/data/etc/")
public class EtcController extends BaseController {

    @Resource
    IEtcService etcService;

    @Resource
    IVehicleRemote vehicleRemote;
    /**
     * 保存etc
     */
    @PostMapping("saveEtcOrder")
    public ReturnData<Object> saveEtcOrder(TransitOrder transitOrder) {
        Account loginUser= UserUtil.getLoginUser(request);
        etcService.saveEtcOrder(transitOrder,loginUser);
        return jsonView(true, "保存成功");
    }
    /**
     * 更新ETC运单
     * 收货后调用
     */
    @PostMapping("updateEtcOrder")
    public ReturnData<Object> updateEtcOrder(TransitOrder transitOrder) {
        etcService.updateEtcOrder(transitOrder);
        return jsonView(true, "保存成功");
    }
    /**
     * AUTO POST
     * 自动实时运单指令
     */
    @PostMapping("autoWaybillStartOrEnd")
    @NoLoginSecurity
    public ReturnData<Object> autoWaybillStartOrEnd() {
        etcService.autoWaybillStartOrEnd(3);
        return jsonView(true, "保存成功");
    }
    /**
     * AUTO POST
     * 自动实时运单指令
     */
    @PostMapping("autoBillingUpdate")
    @NoLoginSecurity
    public ReturnData<Object> autoBillingUpdate() {
        etcService.autoBillingUpdate();
        return jsonView(true, "保存成功");
    }

    /**
     * AUTO POST
     * 车辆自动备案
     */
    @PostMapping("autoVehicleKeep")
    @NoLoginSecurity
    public ReturnData<Object> autoVehicleKeep() {
        etcService.autoVehicleKeep();
        return jsonView(true, "更新成功");
    }

    /**
     * 获取ETC接口余额
     */
    @PostMapping("getBalanceQuery")
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> getBalanceQuery() {
        //获取接口余额：显示 balance -> {Double@1866} 10.0 、lastDeductionTime -> 2020-07-20T10:53:02
        Account loginUser=UserUtil.getLoginUser(request);
        Map<String, Object> map = etcService.balanceQuery(loginUser);
        return jsonView(true, "获取接口余额成功", map);
    }

    /**
     * 功能描述：企业信息查询
     */
    @PostMapping("companyQuery")
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> companyQuery() {
        Account loginUser=UserUtil.getLoginUser(request);
        Map<String, Object> map = etcService.companyQuery(loginUser);
        return jsonView(SUCCESS_CODE, true, "企业信息查询成功", map);
    }

    /**
     * 功能描述：ETC运单发票
     */
    @PostMapping("getEtcOrderPage")
    @UserTarget(UserType.platformUser)
    public ReturnData<PageData<EtcOrder>> getEtcOrderPage(EtcOrder query, Integer page, Integer limit) {
        Account loginUser=UserUtil.getLoginUser(request);
        PageData<EtcOrder> pageData = etcService.getEtcOrderPage(loginUser, query, page, limit);
        for (EtcOrder order : pageData.getList()) {
            order.setCanHistory(new Date().getTime() - order.getStartTime().getTime() > 20 * 24 * 60 * 60 * 1000);
        }
        return jsonView(SUCCESS_CODE, "查询成功", pageData);
    }

    /**
     * 功能描述：手动提交实时运单
     */
    @PostMapping("handWaybillStartOrEnd")
    @NotNull("etcOrderId")
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> handWaybillStartOrEnd(String etcOrderId) {
        Map<String, Object> notQueryBillNo = etcService.handWaybillStartOrEnd(etcOrderId);
        return jsonView(SUCCESS_CODE, true, "提交成功", notQueryBillNo);
    }

    /**
     * 功能描述：查询发票数据
     * TODO:付费
     */
    @PostMapping("invoiceQuery")
    @NotNull("confirmNo")
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> invoiceQuery(String confirmNo) {
        Map<String, Object> map = etcService.invoiceQuery(confirmNo);
        return jsonView(SUCCESS_CODE, true, "企业信息查询成功", map);
    }


    /**
     * 功能描述：车辆备案查询
     */
    @PostMapping("vehicleKeepRecord")
    @NotNull("truckNo")
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> vehicleKeepRecord(@Explain("车牌号") String truckNo) {
        Account loginUser=UserUtil.getLoginUser(request);
        Map<String, Object> map = etcService.vehicleKeepRecord(loginUser.getOrganizeId(), truckNo);
        return jsonView(SUCCESS_CODE, true, "备案查询成功", map);
    }

    /**
     * 功能描述：车辆手動備案
     */
    @PostMapping("vehicleKeep")
    @NotNull("truckNo")
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> vehicleKeep(@Explain("车牌号") String truckNo) {
        Account loginUser=UserUtil.getLoginUser(request);
        Map<String, Object> map = etcService.vehicleKeep(loginUser.getOrganizeId(), truckNo);
        return jsonView(SUCCESS_CODE, true, map.get("ret").toString(), map);
    }

    /**
     * 功能描述：月账单交易查询
     */
    @PostMapping("monthlyBillQuery")
    @NotNull({"taxCode", "tradeMonth", "pageNo"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> monthlyBillQuery(@Explain("税号") String taxCode, @Explain("交易月份") String tradeMonth,
                                               @Explain("页码") String pageNo) {
        Account loginUser=UserUtil.getLoginUser(request);
        Map<String, Object> map = etcService.monthlyBillQuery(loginUser, taxCode, tradeMonth, pageNo);
        return jsonView(SUCCESS_CODE, true, "备案成功", map);
    }

    /**
     * 功能描述：自定义创建历史运单并完成指令
     */
    @PostMapping("createCustomHistoryOrder")
    @NotNull({"num", "plateNumber", "startTime", "endTime", "sourceAddr", "destAddr", "fee"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> createCustomHistoryOrder(EtcOrder etcOrder) {
        Account loginUser=UserUtil.getLoginUser(request);
        EtcOrder orderByNum = etcService.getPartyEtcOrderByNum(etcOrder.getNum());
        ParamUtil.isNull(orderByNum, "ETC运单已存在，若请在列表上发起历史运单开票");
        TruckVehicle truckVehicle = vehicleRemote.getTruckByTruckNo(etcOrder.getPlateNumber()).getData();
        ParamUtil.isNull(truckVehicle, "未找到该车辆");
        ParamUtil.isNull(truckVehicle.getCardPlateCode(), "车辆没有车牌颜色Code");
        if (etcOrder.getPlateColor() == null) {
            String plateColor = etcService.getPlateColor(truckVehicle.getCardPlateCode());
            etcOrder.setPlateColor(plateColor);
        }
        etcService.saveHistoryEtcOrder(etcOrder,loginUser);
        return jsonView(SUCCESS_CODE, true, "提交成功", null);
    }

    /**
     * 功能描述：将失效运单执行为历史运单功能描述：将失效运单执行为历史运单
     */
    @PostMapping("resHistoryOrder")
    @NotNull("etcOrderId")
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> resHistoryOrder(@Explain("ETC运单ID") String etcOrderId) {
        Account loginUser=UserUtil.getLoginUser(request);
        EtcOrder orderByNum = etcService.getEtcOrderById(etcOrderId);
        ParamUtil.notEquals("仅支持失效运单重新提交资料", orderByNum.getEtcStatus(), EtcOrder.EtcStatus.invalid);
        if (new Date().getTime() - orderByNum.getStartTime().getTime() < 20 * 24 * 60 * 60 * 1000) {//20天
            return jsonView(FAIL_CODE, "运单开始时间到现在未超过20天，请在该运单开始时间20条后操作");
        }
        etcService.saveHistoryEtcOrder(orderByNum,loginUser);
        return jsonView(SUCCESS_CODE, true, "重新提交成功", null);
    }

    /**
     * 功能描述：获取未查询过发票的运单编号
     */
    @RequestMapping("getNotQueryBillNo")
    @ResponseBody
    @UserTarget(UserType.platformUser)
    public  ReturnData<Object> getNotQueryBillNo() {
        Account loginUser=UserUtil.getLoginUser(request);
        ArrayList<String> billNoList = etcService.getNotQueryBillNo(loginUser);
        return jsonView(SUCCESS_CODE, true, "查询成功", billNoList);
    }
}
